# 生成的文件合并到同一个csv中
import csv
import numpy as np
import pandas as pd


def checkNum(data):
    non_numeric_mask = np.array([not isinstance(x, (int, float, np.number)) for x in data.flatten()])
    non_numeric_indices = np.where(non_numeric_mask)[0]
    if len(non_numeric_indices) > 0:
        print(f"发现 {len(non_numeric_indices)} 个非数值元素")
        print("非数值元素示例:", data.flatten()[non_numeric_indices[:5]])
    print("检查完毕:", data.flatten()[non_numeric_indices[5:]])


data_N = pd.read_csv('../data/mit/brn52_268/N-train-251020.csv', header=None)
data_S = pd.read_csv('../data/mit/brn52_268/S-train-251020.csv', header=None)
data_V = pd.read_csv('../data/mit/brn52_268/V-train-251020.csv', header=None)
data_F = pd.read_csv('../data/mit/brn52_268/F-train-251020.csv', header=None)
# data_Q = pd.read_csv('../data/mit/brn52_268/Q-train-250929.csv', header=None)

data_N = np.array(data_N)
data_S = np.array(data_S)
data_V = np.array(data_V)
data_F = np.array(data_F)
# data_Q = np.array(data_Q)

checkNum(data_N)
checkNum(data_S)
checkNum(data_V)
checkNum(data_F)
# checkNum(data_Q)

print(f'N类：{data_N.shape}')
print(f'S类：{data_S.shape}')
print(f'V类：{data_V.shape}')
print(f'F类：{data_F.shape}')
# print(f'Q类：{len(data_Q)}')

# 添加类别
new_col_N = np.ones(len(data_N)) * 1
new_col_S = np.ones(len(data_S)) * 2
new_col_V = np.ones(len(data_V)) * 3
new_col_F = np.ones(len(data_F)) * 4
# new_col_Q = np.ones(len(data_Q)) * 5

data_N = np.column_stack((new_col_N, data_N))
data_S = np.column_stack((new_col_S, data_S))
data_V = np.column_stack((new_col_V, data_V))
data_F = np.column_stack((new_col_F, data_F))
# data_Q = np.column_stack((new_col_Q, data_Q))


# , data_Q
data = np.concatenate((data_N, data_S, data_V, data_F), axis=0)
# array_np = np.array(data)

#  打乱顺序
np.random.shuffle(data)
np.random.shuffle(data)
np.random.shuffle(data)
np.random.shuffle(data)
np.random.shuffle(data)
print(f'data：{data.shape}')
with open(f'../data/mit/brn52_268/data_4-train-251020.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data)
